#include "scene2d.h"

Scene2D::Scene2D()
{
    drawer = new TreeDraw();

    shadowGrid = new Shadow2D();

    spaceColTree = new SpaceCol2D();
    tree0 = (TreeGen2D *) spaceColTree->m_tree;
    spaceColTree->generateMarkers();
    shadow = true;
    drawAlgo = true;
    m_addMultipleMeta = true;
    count = 0;
}

Scene2D::Scene2D(Artistic * wg)
{
    drawer = new TreeDraw();
//    tree0 = new TreeGen2D();
//    tree0->genTree();
    shadowGrid = new Shadow2D();
//    tree0->iterateShadowB(shadowGrid);
    spaceColTree = new SpaceCol2D();
    spaceColTree->m_widget = wg;
    tree0 = (TreeGen2D *) spaceColTree->m_tree;

}
void Scene2D::deleteTree(){
    bool b = tree0->in_interact;
 //   delete tree0;
//    tree0 = new TreeGen2D();
//    tree0->genTree();
//    tree0->in_interact = b;

    delete spaceColTree;
    spaceColTree = new SpaceCol2D();
    spaceColTree->m_tree->in_interact = b;
    tree0 = (TreeGen2D *) spaceColTree->m_tree;
    spaceColTree->m_tree->m_addMultMeta = this->m_addMultipleMeta;
}

void Scene2D::iterate(){
    if(!shadow)
        ((TreeGen2D *)spaceColTree->m_tree)->iterateSpaceColD(spaceColTree);
    else
        tree0->iterateShadowB(shadowGrid);
    count ++;
//    if (count == 1)
//    {
//        ((TreeGen2D *)spaceColTree->m_tree)->saveTreeToFile("/home/diemtran/Desktop/test.txt");
//        TreeGen2D * tree = ((TreeGen2D *)spaceColTree->m_tree)->loadTreeFromFile("/home/diemtran/Desktop/test.txt");
//        spaceColTree->m_tree = tree;
//        spaceColTree->m_tree->saveTreeToFile("/home/diemtran/Desktop/test2.txt");
//    }
}

void Scene2D::addMarkers(float x, float y){
    for(int dx = -12; dx <= 12; dx +=4){
        for(int dy = -12; dy <=12; dy +=4){
            spaceColTree->addMarkers(x + dx, y +dy, 0);
        }
    }
}

void Scene2D::toggleInteract(bool b){
    tree0->in_interact = b;
    spaceColTree->m_tree->in_interact = b;

}


Scene2D::~Scene2D(){
    delete drawer;
    delete tree0;
    delete shadowGrid;
}

void Scene2D::initialize()
{
   // spaceColTree->generateMarkers();
   // spaceColTree->m_tree->iterateSpaceColD(spaceColTree);

}

void Scene2D::draw(){

    if(shadow){

        if(drawAlgo)
            shadowGrid->draw();
        drawer->drawTree(tree0);

    }
    else{
        if(drawAlgo){
            spaceColTree->drawMarkers();
        }
        drawer->drawTree((TreeGen2D *)spaceColTree->m_tree);

    }

}

 void Scene2D::addMultipleMetamers(bool b){
    spaceColTree->m_tree->m_addMultMeta = b;
    m_addMultipleMeta = b;
}

bool Scene2D::saveScene(QString filename)
 {
       return ((TreeGen2D *)spaceColTree->m_tree)->saveTreeToFile(filename);
 }

bool Scene2D::loadScene(QString filename)
 {
     TreeGen2D * tree = ((TreeGen2D *)spaceColTree->m_tree)->loadTreeFromFile(filename);
     if (tree == NULL)
         return false;
     spaceColTree->m_tree->freeTree(spaceColTree->m_tree->treeRoot);
     spaceColTree->m_tree = tree;
     tree0 = tree;
     return true;
 }
